package com.sxhuayuan.parking.controller.parkinglot.user;

import com.alibaba.fastjson.JSONObject;
import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.controller.BaseController;
import com.sxhuayuan.parking.entity.Agent;
import com.sxhuayuan.parking.entity.OemConfig;
import com.sxhuayuan.parking.entity.ParkingLot;
import com.sxhuayuan.parking.entity.ParkingLotUser;
import com.sxhuayuan.parking.service.ParkingLotUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author 何龙
 * @create 2019-04-19 11:23
 */
@RestController("parkingLotUserController")
@RequestMapping("/parkinglotuser/parkinglotuser")
public class ParkingLotUserController extends BaseController {
	@Resource(name = "parkingLotUserServiceImpl")
	ParkingLotUserService service;

	/** 删除用户 */
	@RequestMapping("/delete")
	public RespMessage deleteAgent(Long id) {
		if (Objects.isNull(id)) {
			return RespMessage.error(RespMessage.Status.VALID_ERR, Message.FORM_VALID_FAIL);
		}
		service.delete(id);
		return RespMessage.success();

	}

	/** 根据用户名和密码查询 */
	@RequestMapping("/getinfo")
	public RespMessage getCurrent() {
		ParkingLotUser current = service.getCurrent();
		if (current == null) {
			return invalidToken();
		}
		JSONObject user = new JSONObject();
		user.put("username", current.getUsername());
		user.put("type", "parkinglot");
		user.put("name", current.getName());
		List<String> auths = new ArrayList<>();
		current.getRoles().forEach((v) -> auths.addAll(v.getAuthorities()));
		user.put("access", auths);
		Set<ParkingLot> parkingLots = current.getParkingLots();
		if (parkingLots.size() > 0) {
			ParkingLot parkingLot = parkingLots.iterator().next();
			Agent agent = parkingLot.getAgent();
			OemConfig oemConfig = agent.getOemConfig();
			if (oemConfig != null) {
				user.put("maxLogo", oemConfig.getMaxLogo());
				user.put("minLogo", oemConfig.getMinLogo());
				user.put("icon", oemConfig.getIcon());
				user.put("forShort", oemConfig.getForShort());
				user.put("fullname", oemConfig.getFullName());
			}
		}
		return RespMessage.builder().success().setDatas(user).build();
	}

	/** 修改自己的密码 */
	@PostMapping("/changepassword")
	public RespMessage changePassword(@RequestParam String password, @RequestParam String updatePassword) {
		ParkingLotUser current = service.getCurrent();
		if (current == null) {
			return invalidToken();
		}
		String s = DigestUtils.md5Hex(password);

		if (!s.equals(current.getPassword())) {
			return RespMessage.error("原密码输入错误");
		}

		String pattern = "^[^\\s&\"<>]+$";
		if (!(updatePassword.length() >= 4 && updatePassword.length() <= 64 && updatePassword.matches(pattern))) {
			return RespMessage.error("新密码格式不正确");
		}
		current.setPassword(DigestUtils.md5Hex(updatePassword));
		service.update(current);
		return RespMessage.success();
	}


	/** 通过登录密码修改退款密码 */
	@RequestMapping(value = "/change/refund/pass",method = RequestMethod.POST)
	public RespMessage changeRefundPass(String password,String refundPass){
	    //密码已经在前端经过md5加密
		if (StringUtils.isEmpty(password)||StringUtils.isEmpty(refundPass)){
			return RespMessage.error("登录密码和退款密码不能为空");
		}
		ParkingLotUser current = service.getCurrent();
		if (current == null) {
			return RespMessage.error(RespMessage.Status.NON_PERMISSION,"登录失效，请重新登录");
		}
		if (!current.getPassword().equals(password)){
			return RespMessage.error("登录密码错误");
		}
		if (refundPass.length()<4){
			return RespMessage.error("退款密码至少4位");
		}
		current.setRefundPass(refundPass);
		service.update(current);
		return RespMessage.success("修改成功");
	}
}
